layui.use(['table', 'util', 'layer'], function(){
    var table = layui.table,
        util = layui.util,
        layer = layui.layer;

    var paperTable = table.render({
        elem: '#testTable'
        ,url:'/exam/selectExamPaperByPage'
        ,title: '试卷数据表'
        ,method: 'get'
        ,limits: [10,15,20,30]
        ,limit: 10
        ,id:'testTable'
        ,cols: [[
            {field:'id', title:'序号', fixed: 'left',align:'center',type: 'numbers', width: 100, unresize: true, sort: true}
            ,{field:'pname', title:'试卷名',align:'center',fixed: 'left',minWidth: 150}
            ,{field:'pbegintime', title:'开考时间',align:'center', templet: function (res) {
                    return util.toDateString(res.pbegintime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field:'pendtime', title:'结束时间',align:'center', templet: function (res) {
                    return util.toDateString(res.pendtime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field:'pscore', title:'总分',width: 150,align:'center'}
            ,{field:'uname', title:'创建人',align:'center', width: 150}
            ,{fixed: 'right', title:'操作',align:'center',width: 150,  toolbar: '#barDemo' }
        ]]
        ,page: true
        ,response: {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": []
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.data.code, //解析接口状态
                "msg": res.data.msg, //解析提示文本
                "count": res.data.count, //解析数据长度
                "data": res.data.data //解析数据列表
            };
        }
    });

    // 监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'examing'){
            var uId = $('#uId').val();
            $.get(
                "/exam/selectIsFinished/" + data.pid + "/" + uId,
                function (res) {
                    if (res.code == 200) {
                        //查询到数据，表示考过该试卷
                        layer.msg(res.msg, {icon: 2}, {
                            time: 1500
                        });
                    } else {
                        var endtime = util.toDateString(data.pendtime, "yyyy-MM-dd HH:mm:ss");
                        window.location.href= "/examing/" + data.pid + "/" + data.pname + "/" + endtime;
                    }
                }
            )
        }
    });

});
